package com.hm.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.cms.mongo.domain.CmsAdvertisingLink;
import com.hm.cms.mongo.domain.QCmsAdvertisingLink;
import com.hm.cms.mongo.vo.CmsAdvertisingLinkVo;
import com.hm.cms.share.repository.mongo.CmsAdvertisingLinkRepository;
import com.hm.cms.share.service.CmsAdvertisingLinkService;
import com.hm.common.common.BeanUtil;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.model.PageInfo;

/**
 * @author shishun.wang
 * @date 下午2:39:39 2016年8月9日
 * @version 1.0
 * @describe 
 */
@Service
public class CmsAdvertisingLinkServiceImpl implements CmsAdvertisingLinkService {

	@Autowired
	private CmsAdvertisingLinkRepository cmsAdvertisingLinkRepository;

	@Override
	public CmsAdvertisingLinkVo getCmsAdvertisingLink(String id) {
		CmsAdvertisingLink advertisingLink = cmsAdvertisingLinkRepository.findOne(id);
		if (null == advertisingLink) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}

		return (CmsAdvertisingLinkVo) BeanUtil.copyProperties(new CmsAdvertisingLinkVo(), advertisingLink);
	}

	@Override
	public String addCmsAdvertisingLink(CmsAdvertisingLinkVo advertisingLinkVo, String operationUser) {
		CmsAdvertisingLink advertisingLink = new CmsAdvertisingLink();
		{
			advertisingLink = (CmsAdvertisingLink) BeanUtil.copyProperties(new CmsAdvertisingLink(), advertisingLinkVo);
			advertisingLink.setCreateUser(operationUser);
			advertisingLink.setId(null);
		}

		return cmsAdvertisingLinkRepository.save(advertisingLink).getId();
	}

	@Override
	public String updateCmsAdvertisingLink(CmsAdvertisingLinkVo advertisingLinkVo, String operationUser) {
		CmsAdvertisingLink advertisingLink = cmsAdvertisingLinkRepository.findOne(advertisingLinkVo.getId());
		if (null == advertisingLink) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND);
		}
		advertisingLink = (CmsAdvertisingLink) BeanUtil.copyProperties(advertisingLink, advertisingLinkVo);

		return cmsAdvertisingLinkRepository.save(advertisingLink).getId();
	}

	@Override
	public String deleteCmsAdvertisingLink(CmsAdvertisingLinkVo advertisingLinkVo, String operationUser) {
		cmsAdvertisingLinkRepository.delete(advertisingLinkVo.getId());
		return advertisingLinkVo.getId();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PageInfo<CmsAdvertisingLinkVo> queryCmsAdvertisingLink(Integer pageNumber, Integer pageSize) {
		PageInfo<CmsAdvertisingLinkVo> pageInfo = new PageInfo<CmsAdvertisingLinkVo>(pageNumber, pageSize);

		Pageable pageable = new QPageRequest(pageInfo.getPageNumber(), pageInfo.getPageSize(),
				QCmsAdvertisingLink.cmsAdvertisingLink.createTime.desc());
		Page page = cmsAdvertisingLinkRepository.findAll(pageable);

		List<CmsAdvertisingLinkVo> vos = new ArrayList<CmsAdvertisingLinkVo>();
		page.getContent().forEach(baseInfo -> {
			vos.add((CmsAdvertisingLinkVo) BeanUtil.copyProperties(new CmsAdvertisingLinkVo(), baseInfo));
		});

		return pageInfo.execute(page, vos);
	}
	
	
}
